<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-descriptions title="系统定价">
        <el-descriptions-item label="人才股票单价">{{ stockPrice }}元</el-descriptions-item>
      </el-descriptions>
    </el-card>

    <el-table v-loading="loading" :data="stockList">
      <el-table-column align="center" label="股票代码" prop="code" width="200"/>
      <el-table-column align="center" label="股票名称" prop="name" width="200"/>
      <el-table-column align="center" label="最高价" prop="maxPrice"/>
      <el-table-column align="center" label="最低价" prop="minPrice"/>
      <el-table-column align="center" label="最新价格" prop="latestPrice"/>
      <el-table-column align="center" label="未售出股票数量" prop="amount"/>
      <el-table-column align="center" label="当前发行股票总数量" prop="amountSum"/>
      <el-table-column align="center" label="回购状态"  prop="repurchaseVerify" :formatter="repurchaseVerifyFormat"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >回购</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改人才股票对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="80vw">
      <el-descriptions :column="2" border class="margin-top" title="股票信息">
        <el-descriptions-item label="股票代码">{{ form.code }}</el-descriptions-item>
        <el-descriptions-item label="股票名称">{{ form.name }}</el-descriptions-item>
        <el-descriptions-item label="开盘价（￥）">{{ form.openPrice }}</el-descriptions-item>
        <el-descriptions-item label="昨日收盘价（￥）">{{ form.lastClosePrice }}</el-descriptions-item>
        <el-descriptions-item label="最高价（￥）">{{ form.maxPrice }}</el-descriptions-item>
        <el-descriptions-item label="最低价（￥）">{{ form.minPrice }}</el-descriptions-item>
        <el-descriptions-item label="最新价格（￥）"> {{ form.latestPrice }}</el-descriptions-item>
        <el-descriptions-item label="总成交股数">{{ form.volume }}</el-descriptions-item>
        <el-descriptions-item label="总成交金额（￥）">{{ form.turnover }}</el-descriptions-item>
        <el-descriptions-item label="当前未售出股票数量">{{ form.amount }}</el-descriptions-item>
      </el-descriptions>
      <div class="el-descriptions-title">
        是否回购？
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button type="primary" @click="saveForm">回 购</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listFinishStock, getStock, delStock, addStock, updateStock, exportStock,selectUpdateStockList} from "@/api/operate/stock";
  import {getResumeToBelongs,getMyResume} from "@/api/publish/resume";

  export default {
    name: "OngoStock",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 人才股票表格数据
        stockList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 回购进度字典
        repurchaseVerifyOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          code: null,
          name: null,
          openPrice: null,
          lastClosePrice: null,
          maxPrice: null,
          minPrice: null,
          latestPrice: null,
          volume: null,
          turnover: null,
          amount: null,
          amountSum: null,
          repurchaseVerify: null,
          deptId: null,
          createById: null,
          updateById: null,
        },
        // 表单参数
        form: {},
        // 发行人信息
        issuer: {},
        // 表单校验
        rules: {},
        // 股票价格
        stockPrice: 0,
        // 审核意见
      };
    },
    created() {
      this.getList();
      this.getDicts("repurchase_verify").then(response => {
        this.repurchaseVerifyOptions = response.data;
      });
      // 获取股票定价
      getMyResume().then(response => {
        if (response && response.code === 200) {
          this.stockPrice = response.data.score;
        }
      })
    },
    methods: {
      /** 查询人才股票列表 */
      getList() {
        this.loading = true;
        selectUpdateStockList(this.queryParams).then(response => {
          this.stockList = response.rows;
          this.total = response.total;
          console.log(response.rows)
          this.loading = false;
        });
      },
      // 回购进度字典翻译
      repurchaseVerifyFormat(row, column) {
        return this.selectDictLabel(this.repurchaseVerifyOptions, row.repurchaseVerify);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          code: null,
          name: null,
          openPrice: null,
          lastClosePrice: null,
          maxPrice: null,
          minPrice: null,
          latestPrice: null,
          volume: null,
          turnover: null,
          amount: null,
          amountSum: null,
          repurchaseVerify: null,
          deptId: null,
          createBy: null,
          createById: null,
          createTime: null,
          updateBy: null,
          updateById: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 回购按钮 */
      saveForm() {
            if (this.form.id != null) {
              this.form.repurchaseVerify = 1;
              this.form.amountSum -= this.form.amount;
              this.form.amount = 0;
              console.log(this.form)
              updateStock(this.form).then(response => {
                this.msgSuccess("开始进行回购");
                this.open = false;
                this.getList();
              });
            }
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        const _this = this;
        getStock(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "人才股票";
          // 查询发行人简历
          getResumeToBelongs(this.form.owner).then(async response => {
            if (response && response.code === 200) {
              _this.issuer = response.data;
            }
          })
        });
      },
    }
  };
</script>

<style scoped>
  .margin-top {
    margin-top: 20px;
  }
  .el-descriptions-title {
    margin-top: 40px;
    margin-bottom: 20px;
    font-size: 16px;
    font-weight: bold;
  }
</style>
